package com.anolesoft.amljmes.billing.controller;

import com.alibaba.excel.EasyExcel;
import com.anolesoft.amljmes.billing.dto.GdyjDykhxDto;
import com.anolesoft.amljmes.billing.dto.GdyjDykhxInport;
import com.anolesoft.amljmes.billing.listener.GdyjDykhxListener;
import com.anolesoft.amljmes.billing.service.GdyjDykhxService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import lombok.*;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/GdyjDykhx")
@Slf4j
public class GdyjDykhxController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String partDrawingNo;// 图号
        private String factoryId;
    }

    @Autowired
    public GdyjDykhxService gdyjDykhxService;

    //查询
    @PostMapping(value="/query",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryGdyjDykhxInfo(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<GdyjDykhxDto> allList = gdyjDykhxService.queryGdyjDykhxInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<GdyjDykhxDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //新增
    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult addGdyjDykhx(@RequestBody GdyjDykhxDto inFoDtoParam){
        System.out.println(">>>>>>add");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        inFoDtoParam.setCjr(loginUser.getEmpUid());
        inFoDtoParam.setFactoryId(loginUser.getFacCode());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,gdyjDykhxService.addGdyjDykhx(inFoDtoParam));
    }

    // 批量删除
    @PostMapping(value="/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult delete(@RequestBody String[] objuids){
        System.out.println(">>>>>>delete");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,gdyjDykhxService.delete(asList));
    }

    @PostMapping(value="/import")
    public AjaxResult importEqual(MultipartFile file){
        log.debug("文件名：{}", file.getOriginalFilename());
        log.debug("文件大小：{}", file.getSize());

        try {
            EasyExcel.read(file.getInputStream(), GdyjDykhxInport.class, new GdyjDykhxListener(gdyjDykhxService)).sheet(0).doRead();
            return AjaxResult.success("文件导入成功！");
        } catch (IOException e) {
            log.error("导入失败：{}", e.getMessage());
            e.printStackTrace();
            return AjaxResult.success("文件导入异常" + e.getMessage());
        }
    }

    // 验证名称重复
    @PostMapping(value="/Rules", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult isGdyjDykhxName(@RequestBody GdyjDykhxDto inFoDtoPara){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        inFoDtoPara.setCjr(loginUser.getEmpUid());
        inFoDtoPara.setFactoryId(loginUser.getFacCode());

        String partDrawingNo = "";
        int count = gdyjDykhxService.isGdyjDykhxName(inFoDtoPara);
        if(count > 0){
            partDrawingNo = "名称已存在！";
        }
        return new AjaxResult(HttpStatus.SUCCESS, partDrawingNo);
    }
}
